package com.shunwang.caipiao.data.model;

import java.util.ArrayList;
import java.util.List;

import com.shunwang.caipiao.data.pojo.DoubleColor;

public class DataModelMemorySimple implements
		DataModel<DoubleColor, Integer, List<Integer>, String> {
	private final Integer[] info;
	private final List<DoubleColor> objects;
	private int currentRowNum=0;
	private int currentCellNum=0;
	private int maxRowNum=0;
	private int maxCellNum=0;
	private int realMaxRowNum=0;
	private int realMaxCellNum=0;
	
	public DataModelMemorySimple(List<DoubleColor> list){
		if(list == null || list.size() < 0){
			throw new IllegalArgumentException("init DataModelMemorySimple fail,because param doubleColor is empty");
		}
		objects = list;
		maxCellNum = 10;
		maxRowNum = list.size() -1;
		realMaxRowNum = maxRowNum;
		realMaxCellNum = maxCellNum;
		info = new Integer[maxCellNum * (maxRowNum+1)];
		
		for(int i=0,len=list.size(),j=0;i<len;i++){
			DoubleColor color = list.get(i);
			info[j++]=color.getRows();
			info[j++]=color.getRed_1();
			info[j++]=color.getRed_2();
			info[j++]=color.getRed_3();
			info[j++]=color.getRed_4();
			info[j++]=color.getRed_5();
			info[j++]=color.getRed_6();
			info[j++]=color.getBule_1();
			info[j++]=color.getBule_2() == null?-1:color.getBule_2();
			info[j++]=color.getSno();
		}
	}
	@Override
	public DataModel moveRow(int rowNum) {
		checkRow(rowNum);
		this.currentRowNum = rowNum;
		return this;
	}

	@Override
	public DataModel moveCell(int cellIndex, int rowNum) {
		checkRow(rowNum);
		checkCell(cellIndex);
		this.currentRowNum = rowNum;
		this.currentCellNum = cellIndex;
		return this;
	}

	@Override
	public DataModel moveRowByKey(String key) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DataModel moveCellByKey(String key, int cellIndex) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public DataModel moveNextRow() {
		int nextRow = this.currentRowNum + 1;
		checkRow(nextRow);
		this.currentRowNum = nextRow;
		return this;
	}

	@Override
	public DataModel moveNextCell() {
		int nextCell = this.currentCellNum + 1;
		checkCell(nextCell);
		this.currentCellNum = nextCell;
		return this;
	}

	@Override
	public DoubleColor getCurrentRow() {
		return objects.get(this.currentRowNum);
	}

	@Override
	public Integer getCurrentCell() {
		int index = currentRowNum * maxCellNum + currentCellNum;
		return info[index];
	}

	@Override
	public List<Integer> getCellData(int cellIndex) {
		return getCellInfo(cellIndex,0,maxRowNum);
		
	}
	
	@Override
	public List<List<Integer>> getCellDatas(int[] cellIndex) {
		return getCellInfoArray(cellIndex,0,this.maxRowNum);
	}
	

	
	@Override
	public List<Integer> getCellDataAfter(int cellIndex) {
		return getCellInfo(cellIndex,this.currentRowNum,this.maxRowNum);
	}

	@Override
	public List<List<Integer>> getCellDatasAfter(int[] cellIndex) {
		return getCellInfoArray(cellIndex,this.currentRowNum,this.maxRowNum);
	}

	@Override
	public List<Integer> getCellDataBefore(int cellIndex) {
		return getCellInfo(cellIndex,0,this.currentRowNum);
	}

	@Override
	public List<List<Integer>> getCellDatasBefore(int[] cellIndex) {
		return getCellInfoArray(cellIndex,0,this.currentRowNum);
	}

	@Override
	public List<DoubleColor> getCurrentRowsBefore() {
		return objects.subList(0, this.currentRowNum);
	}

	@Override
	public List<DoubleColor> getCurrentRowsAfter() {
		return objects.subList(this.currentRowNum, this.maxRowNum);
	}
	@Override
	public DataModel moveFirstRow() {
		this.currentRowNum = 0;
		return this;
	}
	@Override
	public DataModel moveLastRow() {
		this.currentRowNum = this.maxRowNum;
		return this;
	}
	/**
	 * 检查row有效性
	 * @param rowNum
	 */
	public void checkRow(int rowNum){
		if(realMaxRowNum < rowNum || rowNum < 0){
			throw new IllegalArgumentException("DataModelMemorySimple row num illegal,max row num is "+maxRowNum);
		}
	}
	/**
	 * 检查单元格有效性
	 * @param cellNum
	 */
	public void checkCell(int cellNum){
		if(realMaxCellNum < cellNum || cellNum < 0){
			throw new IllegalArgumentException("DataModelMemorySimple cell num illegal,max cell num is "+maxCellNum);
		}
	}
	/**
	 * 检查并取出array中无效数据
	 * @param cellIndex
	 * @return
	 */
	private int[] checkParamArray(int[] cellIndex) {
		if(cellIndex == null || cellIndex.length < 1){
			throw new IllegalArgumentException("checkParamArray fail,make sure array not empty");
		}
		List<Integer> del = new ArrayList<Integer>();
		for(int i = 0,len = cellIndex.length;i<len;i++){
			if(cellIndex[i] > maxCellNum || cellIndex[i] < 0){
				del.add(i);
			}
		}
		cellIndex = reStructorArray(cellIndex,del);
		if(cellIndex == null || cellIndex.length < 1){
			throw new IllegalArgumentException("checkParamArray fail,make sure array not empty");
		}
		return cellIndex;
	}
	/**
	 * 取出array中无效数据
	 * @param cellIndex
	 * @param del
	 * @return
	 */
	private int[] reStructorArray(int[] cellIndex, List<Integer> del) {
		if(del.size() < 1){
			return cellIndex;
		}
		int[] newCell = new int[cellIndex.length-del.size()];
		for(int i=0,j=0;i<cellIndex.length;i++){
			if(del.contains(i)){
				continue;
			}
			newCell[j] = cellIndex[i];
			j++;
		}
		return newCell;
	}
	/**
	 * 获取Row区间内的某个单元格
	 * @param cellIndex
	 * @param startRow
	 * @param endRow
	 * @return
	 */
	private List<Integer> getCellInfo(int cellIndex,int startRow,int endRow) {
		checkRow(startRow);
		checkRow(endRow);
		checkRange(startRow,endRow);
		List<Integer> cellList = new ArrayList<Integer>(endRow - startRow);
		for(int i=startRow,len = endRow;i<len;i++){
			int index = i*maxCellNum+cellIndex;
			cellList.add(info[index]);
		}
		return cellList;
	}
	/**
	 * 获取Row区间多个cell信息
	 * @param cellIndex
	 * @param startRow
	 * @param endRow
	 * @return
	 */
	private List<List<Integer>> getCellInfoArray(int[] cellIndex,int startRow,int endRow) {
		checkRow(startRow);
		checkRow(endRow);
		checkRange(startRow, endRow);
		cellIndex = checkParamArray(cellIndex);
		List<List<Integer>> cellList = new ArrayList<List<Integer>>(maxRowNum);
		for(int i=startRow,len = endRow;i<len;i++){
			List<Integer> itemList= new ArrayList<Integer>();
			for(int cell:cellIndex){
				int index = i*maxCellNum+cell;
				itemList.add(info[index]);
			}
			cellList.add(itemList);
		}
		return cellList;
	}
	/**
	 * 检查开始和结束是否正常
	 * @param startRow
	 * @param endRow
	 */
	private void checkRange(int startRow, int endRow) {
		if(startRow < 0 || startRow > endRow){
			throw new IllegalArgumentException("startRow and endRow ranage zone fail");
		}
	}
	public int getMaxRowNum() {
		return maxRowNum;
	}
	public int getMaxCellNum() {
		return maxCellNum;
	}
	public void limitRowMax(int rowNum){
		checkRow(rowNum);
		this.maxRowNum = rowNum;
	}
	public void limitCellMax(int cellNum){
		checkCell(cellNum);
		this.maxCellNum = cellNum;
	}
	public void resetRowMax(){
		this.maxRowNum = this.realMaxRowNum;
	}
	/**
	 * 重置cell数量
	 */
	public void resetCellMax(){
		this.maxCellNum = this.realMaxCellNum;
	}
	public int getCurrentRowNum() {
		return currentRowNum;
	}
	public int getCurrentCellNum() {
		return currentCellNum;
	}
	
	
}
